/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.woody.aggregate.app.config;

import com.woody.base.constant.HttpCodeEnum;
import com.woody.base.exception.BaseException;
import com.woody.base.model.Response;
import com.woody.common.web.util.CommWebUtil;
import feign.RetryableException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.net.SocketTimeoutException;

/**
 * 自定义错误处理器
 * @author LGH
 */
@Slf4j
@RestControllerAdvice
public class AggregateDefaultExceptionHandlerConfig {

    /**
     * @param e:
     * @Version 1.0
     * @Description 处理业务错误
     */
    @ExceptionHandler(BaseException.class)
    public ResponseEntity<Object> baseExceptionHandler(BaseException e) {
        log.info("baseException: {}", e.getMessage());
        Response response = new Response(e.getCode(), e.getMsg(), null);
        return ResponseEntity.status(HttpCodeEnum.OK.getCode()).body(response);
    }

    @ExceptionHandler(SocketTimeoutException.class)
    public Response socketTimeoutExceptionHandle(SocketTimeoutException e) {
        log.error("SocketTimeoutException: ", e.getMessage());
        HttpCodeEnum timeOut = HttpCodeEnum.TIME_OUT;
        Response response = new Response(timeOut.getCode(), timeOut.getMessage(), null);
        return response;
    }

    /**
     ** 统一捕获并提示超时异常
     */
    @ExceptionHandler({RetryableException.class,})
    public Response retryableExceptionHandle(RetryableException e) {
        log.error("retryableExceptionHandle: ", e);
        HttpCodeEnum timeOut = HttpCodeEnum.TIME_OUT;
        Response response = new Response(timeOut.getCode(), timeOut.getMessage(), null);
        return response;
    }

    @ExceptionHandler(BindException.class)
    public Response bindExceptionHandler(BindException e){
        log.error("BindExceptioner:", e);
        Response response = Response.fail(e.getBindingResult().getFieldErrors().get(0).getDefaultMessage());
        response.setCode(HttpStatus.BAD_REQUEST.value());
        return response;
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Response methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e){
        log.error("MethodArgumentNotValidException:", e);
        Response response = Response.fail(e.getBindingResult().getFieldErrors().get(0).getDefaultMessage());
        response.setCode(HttpStatus.BAD_REQUEST.value());
        return response;
    }

    @ExceptionHandler(Exception.class)
    public Response exception(Exception e){
        Response response =Response.fail(CommWebUtil.errorLog(e));
        return response;
    }
}
